package com.wanxi.service.Impl;

import com.wanxi.dao.Impl.ProductTypeDaoImpl;
import com.wanxi.dao.ProductTypeDao;
import com.wanxi.dto.ResultDTO;
import com.wanxi.model.NewsType;
import com.wanxi.model.ProductType;
import com.wanxi.service.ProductTypeService;
import com.wanxi.util.JSONCopy;
import com.wanxi.vo.ProductTypeVO;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ProductTypeServiceImpl implements ProductTypeService {
    ProductTypeDao productTypeDao=new ProductTypeDaoImpl();
    @Override
    public ResultDTO findAllType() {
        List<ProductType> list=productTypeDao.findAllType();
        if(list.size()>0){
            return ResultDTO.getDTO(list.size(),list);
        }
        return ResultDTO.builder().data(list).msg("no data").count(0).code(1).build();
    }

    @Override
    public ResultDTO findProductTypeById(ProductTypeVO productTypeVO) {
        ProductType productType= JSONCopy.copy(productTypeVO, ProductType.class);
        ProductType pt=productTypeDao.findProductTypeById(productType);
        if(pt!=null){
            return ResultDTO.getDTO(1,pt);
        }
        return ResultDTO.builder().count(0).msg("error").code(1).build();
    }

    @Override
    public ResultDTO updateEnable(ProductTypeVO productTypeVO) {
        ProductType productType= JSONCopy.copy(productTypeVO, ProductType.class);
        int i=productTypeDao.updateEnable(productType);
        if(i==1){
            return ResultDTO.getDTO(i,productTypeVO);
        }
        return ResultDTO.builder().data(productTypeVO).count(0).code(1).msg("error").build();
    }

    @Override
    public ResultDTO addProductType(ProductTypeVO productTypeVO) {
        ProductType productType= JSONCopy.copy(productTypeVO, ProductType.class);
        int i=productTypeDao.addProductType(productType);
        if(i==1){
            return ResultDTO.getDTO(i,productTypeVO);
        }
        return ResultDTO.builder().data(productTypeVO).count(0).code(1).msg("error").build();
    }

    @Override
    public ResultDTO deleteProductType(ProductTypeVO productTypeVO) {
        ProductType productType= JSONCopy.copy(productTypeVO, ProductType.class);
        int i=productTypeDao.deleteProductType(productType);
        if(i==1){
            return ResultDTO.getDTO(i,productTypeVO);
        }
        return ResultDTO.builder().data(productTypeVO).count(0).code(1).msg("error").build();
    }

    @Override
    public ResultDTO updateProductType(ProductTypeVO productTypeVO) {
        ProductType productType= JSONCopy.copy(productTypeVO, ProductType.class);
        int i=productTypeDao.updateProductType(productType);
        if(i==1){
            return ResultDTO.getDTO(i,productTypeVO);
        }
        return ResultDTO.builder().data(productTypeVO).count(0).code(1).msg("error").build();
    }

    @Override
    public ResultDTO findType() {
        List<ProductType> list=productTypeDao.findAllType();
        if(list.size()>0){
            Iterator<ProductType> iterator=list.iterator();
            while (iterator.hasNext()){
                ProductType pt= iterator.next();
                if (pt.getEnable()==0)
                    iterator.remove();
            }
            return ResultDTO.getDTO(list.size(),list);
        }
        return ResultDTO.builder().data(list).msg("no data").count(0).code(1).build();
    }
}
